const query=require('../mysql/index')
const {success,error}=require('../model/index')

class ShopController {
    //数据
    getShopList=async (ctx)=>{
        const {keyword,sortBy,sortType,pageSize,pageIndex}=ctx.query;
        let sql=`select * from shopdata where 1=1 ` 
        let sqlCount=`select count(id) as total from shopdata where 1=1 `
       //模糊查询
        if(keyword){
            sql+=`and title like "%${keyword}%" `
            sqlCount+=`and title like "%${keyword}%" `
        }
        //排序
        if(sortBy && sortType){
            sql+=`order by ${sortBy} ${sortType} `
        }
        //分页
        if(pageIndex && pageSize){
            sql+=`limit ${(pageIndex-1)*pageSize},${pageSize} `
            sqlCount+=`limit ${(pageIndex-1)*pageSize},${pageSize} `
        }
        const data=await query(sql)
        const count=await query(sqlCount)
        ctx.body=success({
            data:data,
            count:count[0].total
        })
    }
    //详情
    getDetail=async (ctx)=>{
        const {id}=ctx.params;
        const sql=`select * from shopdata where id=${id}`
        const data=await query(sql)
        console.log(data)
        ctx.body=success(data)
    }
}

module.exports=new ShopController()